package Servlets;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.mortbay.util.ajax.Continuation;
import org.mortbay.util.ajax.ContinuationSupport;


import Server.ServerProtocol;


public class Servlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	
	
	public void service(HttpServletRequest req, HttpServletResponse resp){
		System.out.println("!!!!!!!!!in servlet!!!!!!!!!");
		Continuation cont = null;
		String userName=null;
		try {
			ObjectInputStream in = new ObjectInputStream(req.getInputStream());
			userName = (String) in.readObject();
			in.close();
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		cont = (Continuation) ContinuationSupport.getContinuation(req, null);
		ServerProtocol serverProtocol = ServerProtocol.getInstance();
		System.out.println("in servlet, user name is "+userName);
		serverProtocol.addContinuation(cont, userName);
		ObjectOutputStream out;
		try {
			out = new ObjectOutputStream(resp.getOutputStream());
			out.writeObject("good");
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	//	public Continuation getCont() {
//		return cont;
//	}
	

}
